package chances.framework.jpa.config;

import java.text.SimpleDateFormat;

import javax.persistence.EntityManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig;

@Import(RepositoryRestMvcConfiguration.class)
@ComponentScan("chances.ams.program.ctrl")
public class RestMvcConfiguration extends RepositoryRestConfigurerAdapter {

	@Autowired
	EntityManager entityManager;

	private static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Override
	public void configureJacksonObjectMapper(ObjectMapper objectMapper) {
		SerializationConfig serializationConfig = objectMapper.getSerializationConfig().with(df);
		objectMapper.setConfig(serializationConfig);
	}

	@Override
	public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
		// TODO Auto-generated method stub
		super.configureRepositoryRestConfiguration(config);
		entityManager.getMetamodel().getEntities().forEach(s->{
			config.exposeIdsFor(s.getJavaType());
		});
		/*config.exposeIdsFor(EnumData.class, Organization.class, Permission.class, Role.class, Operator.class, Product.class,
				Partner.class, Plan.class);*/
	}

}
